using System;
using AutoMapper;
using Web.App.Infrastructure.Context.Contracts;
using Webshop20.Application.DataTransferObjects;
using Webshop20.Application.Queries.Messages;
using Webshop20.Domain.Customers.Core;
using Webshop20.Domain.Security.Authentication.Core;
using Web.App.Infrastructure.ObjectStore.Contracts;

namespace Webshop20.Application.Queries.Services
{
    public interface IGetNewRegistrationDataService : IService{}

    public class GetNewRegistrationDataService : ServiceBase, IGetNewRegistrationDataService
    {
        private readonly IObjectStoreRepository objectStore;

        public GetNewRegistrationDataService(IObjectStoreRepository objectStore)
        {
           
            this.objectStore = objectStore;
        }

        public override ISystemMessage Handle(ISystemMessage request)
        {
            var newRegistrationDataRequest = request as GetNewRegistrationDataRequest;
            var accountInProgress = newRegistrationDataRequest.AccountInterimKey != null ? objectStore.Find(new Guid(newRegistrationDataRequest.AccountInterimKey)).Data as Account : null;
            var customerInProgress = newRegistrationDataRequest.CustomerInterimKey != null ? objectStore.Find(new Guid(newRegistrationDataRequest.CustomerInterimKey)).Data as Customer : null;
            

            var newRegistrationDTO = Mapper.Map<Account, NewRegistrationDTO>(accountInProgress);
            Mapper.Map(customerInProgress, newRegistrationDTO);
            return new GetNewRegistrationDataResponse(newRegistrationDTO);
        }
    }
}